package com.base.service.impl;

import java.util.List;

import com.base.service.FlowtemplatebranchService;
import com.common.bean.Flowtemplatebranch;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;

public class FlowtemplatebranchServiceImpl extends AbstractManager implements
		FlowtemplatebranchService {

	public void addFlowtemplatebranch(Flowtemplatebranch flowtemplatebranch) {
		String hql = "select nvl(max(branchid),0) from Flowtemplatebranch";
		
		long branchid = 0l;
		branchid = (Long) this.getHibernateTemplate().find(hql).get(0);
		branchid += 1l;
		
		flowtemplatebranch.setBranchid(branchid);
		this.getHibernateTemplate().save(flowtemplatebranch);
	}

	@SuppressWarnings("unchecked")
	public List<Flowtemplatebranch> findAllFlowtemplatebranch() {
		String hql = "from Flowtemplatebranch order by branchid";
		return this.getHibernateTemplate().find(hql);
	}

	public Flowtemplatebranch findFlowtemplatebranch(long branchid) {
		return (Flowtemplatebranch) this.getHibernateTemplate().load(Flowtemplatebranch.class, branchid);
	}

	public PagerModel findFlowtemplatebranchForPager() {
		String hql = "from Flowtemplatebranch order by branchid";
		return searchPaginated(hql);
	}

	public void removeFlowtemplatebranch(long branchid) {
		Flowtemplatebranch flowtemplatebranch = null;
		flowtemplatebranch = (Flowtemplatebranch) this.getHibernateTemplate().load(Flowtemplatebranch.class, branchid);
		if(flowtemplatebranch != null) {
			this.getHibernateTemplate().delete(flowtemplatebranch);
		}
	}

	public void updataFlowtemplatebranch(Flowtemplatebranch flowtemplatebranch) {
		this.getHibernateTemplate().update(flowtemplatebranch);
	}

	@SuppressWarnings("unchecked")
	public List findAllFlowtemplatebranchWith(long workflowid) {
		String hql = "select branch.branchid,branch.branchseq,branch.cardsubtype.cardsubtypename," +
				"branch.department.deptcaption,branch.rule.rulename,branch.runpoint," +
				"branch.defaultrun,branch.datadict.name from Flowtemplatebranch branch " +
				"where branch.flowtemplatedetail.workflowid = ? order by branch.cardsubtype.cardsubtypename, branch.branchseq";
		return this.getHibernateTemplate().find(hql,workflowid);
	}

}
